package com.maddox.relax.camera.c1

import android.content.Context
import android.graphics.ImageFormat
import android.hardware.Camera
import android.view.SurfaceHolder
import android.view.SurfaceView
import com.maddox.relax.util.LogUtil
import java.util.*
import kotlin.collections.ArrayList

/**
 * @author Maddox
 * 相机预览使用布局封装
 */
class CameraPreview(context: Context, private val mCamera: Camera) : SurfaceView(context),
    SurfaceHolder.Callback {

    private val TAG = CameraPreview::class.java.simpleName

    private val mHolder: SurfaceHolder = holder.apply {
        addCallback(this@CameraPreview)
        setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
    }


    override fun surfaceCreated(holder: SurfaceHolder?) {
        LogUtil.d("surfaceCreated:width:${width} height:${height}")
        startPreview()
    }

    override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
        LogUtil.d("surfaceChanged")
        //预览界面改变,进行处理
        if (mHolder.surface == null) return
        //停止预览
        try {
            mCamera.stopPreview()
        } catch (e: Exception) {
        }
        //重新设置相机的一些参数 预览尺寸,旋转
        //开启预览
        startPreview()

    }


    override fun surfaceDestroyed(holder: SurfaceHolder?) {
        LogUtil.d("surfaceDestroyed")
        try {
            mCamera.stopPreview()
        } catch (e: Exception) {
        }
    }

    private fun startPreview() {
        mCamera.apply {
            try {
                initCamera()
                setPreviewDisplay(holder)
                startPreview()
            } catch (e: Exception) {
                LogUtil.d("开启相机预览异常($TAG)->surfaceCreated:${e.message}")
            }
        }
    }

    //设置相机的一些参数
    private fun initCamera() {
        mCamera.cancelAutoFocus()
        var parameters = mCamera.parameters
        parameters.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
        parameters.pictureFormat = ImageFormat.JPEG
        parameters.jpegQuality = 100
        //设置预览尺寸,拍照图片尺寸
        val bestPreviewSize = getBestPreviewSize(parameters)
        parameters.setPreviewSize(bestPreviewSize.width, bestPreviewSize.height)
        LogUtil.d("设置预览尺寸:${bestPreviewSize.width}x${bestPreviewSize.height}")
        val bestPictureSize = getBestPictureSize(parameters)
        parameters.setPictureSize(bestPictureSize.width, bestPictureSize.height)
        LogUtil.d("设置图片尺寸:${bestPictureSize.width}x${bestPictureSize.height}")
        mCamera.parameters = parameters
        mCamera.autoFocus { success, camera ->
            if (success) {
                LogUtil.d("对焦成功")

            }
        }

    }

    private fun getBestPictureSize(parameters: Camera.Parameters): Camera.Size {
        val supportSizes = parameters.supportedPictureSizes
//        LogUtil.d("**************************************图片尺寸****************************************")
        val maxSize = Math.max(width,height)
        for (size in supportSizes) {
            if (maxSize<=Math.max(size.width,size.height)) {
                return size
            }
//            LogUtil.d("supportPicSize:width:${size.width} height:${size.height}")
        }
        return supportSizes[supportSizes.size / 2]
    }

    private fun getBestPreviewSize(parameters: Camera.Parameters): Camera.Size {
        val supportSizes = parameters.supportedPreviewSizes
//        LogUtil.d("**************************************预览尺寸****************************************")
        val maxSize = Math.max(width,height)
        for (size in supportSizes) {
            if (maxSize<=Math.max(size.width,size.height)) {
                return size
            }
//            LogUtil.d("supportPreSize:width:${size.width} height:${size.height}")
        }
        return supportSizes[supportSizes.size / 2]
    }

    //释放相机资源
     fun releaseCamera() {
        try {
            mCamera.stopPreview()
            mCamera.release()
        } catch (e: java.lang.Exception) {
        }
    }



}